import 'package:flutter/foundation.dart';
import 'package:imperius_insights/modules/scope/domain.dart';
import 'package:imperius_insights/portal/env/environments.dart';
import 'package:imperius_insights/portal/state/mobx/store.dart';
import 'package:imperius_insights/treaty/endpoint.dart';
import 'package:mobx/mobx.dart';

import 'service.dart';
import 'typings.dart';

part 'store.g.dart';

typedef OnCheckingTarget = Function(EnumCheckingTarget checkingTarget);

class DiagnosticStore = _DiagnosticStore with _$DiagnosticStore;

enum EnumCheckingTarget {
  NetworkInfo,
  ConnectivityInfo,
  Measurements,
  HealthCheck,
  Completed
}

abstract class _DiagnosticStore with Store, StoreWithLifecycle {
  Service _service;

  _DiagnosticStore() {
    _service = Service();
  }

  List<MeasurementRequest> get measureRequests {
    if (isWebRuntime) {
      return <MeasurementRequest>[
        MeasurementRequest('Imperius代理服务',
            uri: endpointWeb),
      ];
    }
    return <MeasurementRequest>[
      MeasurementRequest('QUNR内网', uri: 'https://qunar.it'),
      MeasurementRequest('奥丁域名',
          uri: 'http://qa.corp.qunar.com'),
    ];
  }

  List<HealthCheckRequest> get healthCheckRequests {
    return [
      HealthCheckRequest('排期甘特图接口', queryProjectsPlan, params: {
        'param': DEFAULT_DEPARTMENT_ID,
        'parent': '2',
        'typew': '',
      }),
      HealthCheckRequest('项目信息详查接口', queryPmoInfo,
          params: {'pmo': 'FLIGHT-113318'}, dataFormat: EnumDataFormat.Unspecific),
      HealthCheckRequest('todolist项目接口', queryCaseCount,
          params: {'projectId': 'FLIGHT-113318'}),
//      HealthCheckRequest('项目信息查询接口',
//          WebCallableTarget(host: '', relativePath: '')),
//      HealthCheckRequest('英普瑞斯数据分析',
//          WebCallableTarget(host: '', relativePath: '')),
    ];
  }

  launchDiagnostic(
      {@required OnCheckingTarget onCheckingTarget,
      @required OnNetworkInfoResolved onNetworkInfoResolved,
      @required OnConnectivityInfoResolved onConnectivityInfoResolved,
      @required OnRequestMeasured onRequestMeasured,
      @required OnApiHealthChecked onApiHealthChecked}) async {
//    final networkInfo = await _service.queryNetworkInfo();
//    onCheckingTarget(EnumCheckingTarget.NetworkInfo);
//    if (!onNetworkInfoResolved(networkInfo)) {
//      onCheckingTarget(EnumCheckingTarget.Completed);
//      return;
//    }
    onCheckingTarget(EnumCheckingTarget.ConnectivityInfo);
    final connectivityInfo = await _service.resolveConnectivityInfo();
    if (!onConnectivityInfoResolved(connectivityInfo)) {
      onCheckingTarget(EnumCheckingTarget.Completed);
// recognize web      return;
    }
    onCheckingTarget(EnumCheckingTarget.Measurements);
    await _service.measureNetworkPerformance(
        measureRequests, onRequestMeasured);
    onCheckingTarget(EnumCheckingTarget.HealthCheck);
    await _service.healthCheckApis(healthCheckRequests, onApiHealthChecked);
    onCheckingTarget(EnumCheckingTarget.Completed);
  }
}
